- 首先安装pytest
pip install pytest
- 安装pytest的allure插件
pip install pytest-allure-adaptor
-
安装allure,因为allure依赖于java环境,所以必须先安装java环境并设置环境变量(自行百度就可以)。
-
去以下链接,下载一个包,解压后将bin路径设置到环境变量path中
https://github.com/allure-framework/allure2/releases
path环境变量如下图所示,
这样就可以通过cmd中的allure命令来生成测试报告 -
我在测试过程中提示module ‘pytest’ has no attribute ‘allure’,以下这篇文章可以解决:
https://www.cnblogs.com/lansan0701/p/10345142.html -
装好以后,来写个demo
#coding=utf-8
import pytest
import allure
marketid = ['101','102']
testlist = ['1','test','test1']
testlist2 = ['test0','test2','test3']
#市场id对应也生成相同的数组
@pytest.fixture(scope='module',autouse=True)
@allure.feature('测试准备')
def prepare():
print('这是测试准备哦')
yield
print('module 测试结束')
@allure.feature('期权强平测试')
class Test_module1:
@allure.feature('期权市价委托强平')
@pytest.mark.parametrize('str1',testlist)
@pytest.mark.parametrize('str2',testlist2)
def test1(self,str1,str2):
"""
用例描述:期权强平市价委托
"""
stradd = str1 + str2
print('这是test1')
assert 1 == 2
@allure.feature('期权限价委托强平')
def test2(self):
with allure.step("调节持仓"):
x = '1'
allure.attach(x)
print('这是test2')
with allure.step('进行强平的操作'):
print('DTE')
class Test_module2:`在这里插入代码片`
def test3(self):
with allure.step("调节持仓"):
x = '1'
allure.attach(x)
print('这是test2')
with allure.step('进行强平的操作'):
print('DTE')
我用的是pycharm,在pycharm的Terminal中输入命令:
pytest -s -q --alluredir report
这时候生成的是一堆json文件,不方便查看,大概长这样:
需要用刚才安装的allure command-line工具生成html格式的测试报告,在cmd中输入:
allure generate --clean report
来看一下生成的报告路径:
打开上面图那个html,看一眼呗:(如果要用chrome打开,则必须在pycharm右键使用chrome打开,如果直接打开文件的话会404,不清楚原因,不过直接用firefox浏览器打开文件可以正常显示)
allure的其他用法,可以去官网看它的document和examples:
http://allure.qatools.ru/
行吧,测试报告就先整成这样吧!
https://blog.csdn.net/DaxiaLeeSuper/article/details/94138029
这篇文章中写了如何该脚本配置Jenkins执行,有兴趣可以看下。